package com.itheima.service.impl;

import com.itheima.common.R;
import com.itheima.common.TokenHolder;
import com.itheima.dto.AddDishDTO;
import com.itheima.mapper.DishFlavorMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.DishService;
import com.itheima.vo.DishAndCategoryVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 小帅蓝
 * @Date 2022/6/16 18:56
 */
@Service
public class DishServiceImpl implements DishService {

    @Resource
    private DishMapper dishMapper;
    @Resource
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public Integer getCount(String name) {
        return dishMapper.getCount(name);
    }

    @Override
    public List<DishAndCategoryVO> query(String name, Integer page, Integer pageSize, String type) {
        Integer offset = getOffset(page, pageSize);
        return dishMapper.query(name, offset, pageSize, type);
    }

    @Override
    public List<DishAndCategoryVO> selectList(String categoryId, Integer status) {
        //先查找dish
        List<DishAndCategoryVO> vos = dishMapper.selectList(categoryId, status);
        //查询口味
        for (DishAndCategoryVO vo : vos) {
            String dishId = vo.getId();
            List<DishFlavor> flavorList = this.dishFlavorMapper.selectByDishId(dishId);
            vo.setFlavors(flavorList);
        }
        return vos;
    }

    @Override
    public R<String> addDish(AddDishDTO addDishDTO) {
        List<DishFlavor> flavors = addDishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(addDishDTO.getId());
        }

        //获取登录者id
        String employeeId = TokenHolder.getCurrentId();
        addDishDTO.setCreateUser(employeeId);
        addDishDTO.setUpdateUser(employeeId);

        int result = dishMapper.addDish(addDishDTO);
        if (result > 0) {
            return R.success("新增菜品成功!");
        } else {
            return R.error("增加菜品失败！");
        }
    }

    @Override
    public DishAndCategoryVO queryById(String id) {
        DishAndCategoryVO dishAndCate = dishMapper.queryById(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishId(id);
        dishAndCate.setFlavors(dishFlavors);
        return dishAndCate;
    }

    @Override
    public R<String> updateDish(AddDishDTO addDishDTO) {
        List<DishFlavor> flavors = addDishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setId(addDishDTO.getId());
        }

        String employeeId = TokenHolder.getCurrentId();
        addDishDTO.setUpdateUser(employeeId);
        int result = dishMapper.updateDish(addDishDTO);
        if (result > 0) {
            return R.success("修改菜品成功!");
        } else {
            return R.error("修改菜品失败！");
        }
    }

    @Override
    public boolean deleteDish(String ids) {
        String[] split = ids.split(",");
        Integer result = dishMapper.deleteDish(split);
        return result.equals(split.length);
    }

    @Override
    public boolean updateStatus(String ids, Integer status) {
        String[] idsArray = ids.split(",");
        int result = dishMapper.updateStatus(idsArray, status);
        return result == idsArray.length;
    }

    /**
     * 获取起始页
     *
     * @param page     当前页
     * @param pageSize 每页条数
     * @return 每页起始数
     */
    public Integer getOffset(Integer page, Integer pageSize) {
        return (page - 1) * pageSize;
    }
}
